home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / boot / aix_foreign.pl next >
Encoding:
Text File  |  1995-08-16  |  3.7 KB  |  166 lines

  1. /*  $Id: aix_foreign.pl,v 1.4 1995/08/16 11:49:17 jan Exp $
  2.  
  3.     Copyright (c) 1991 Jan Wielemaker. All rights reserved.
  4.     jan@swi.psy.uva.nl
  5.  
  6.     Purpose: Foreign interface for AIX version
  7. */
  8.  
  9. :- module($aix_foreign,
  10.     [ load_foreign/5
  11.     , load_foreign/2
  12.     , load_foreign/1
  13.     , foreign_file/1
  14.     ]).
  15.  
  16. :- module_transparent
  17.     load_foreign/2,
  18.     load_foreign/1.
  19.  
  20. %    load_foreign/5
  21. %    Not available on AIX.  Just print an error
  22.  
  23. load_foreign(_, _, _, _, _) :-
  24.     $warning('load_foreign/5: not on AIX; see load_foreign/[1,2]'),
  25.     fail.
  26.  
  27.  
  28. %    load_foreign(+Executable)
  29. %    Load a foreign file and call its entry point.
  30.  
  31. load_foreign(File) :-
  32.     statistics(heapused, OldHeap),
  33.     statistics(cputime, OldTime),
  34.  
  35.     (   absolute_file_name(File, [access(read)], Path)
  36.     ->  true
  37.     ;   $warning('~w: No such foreign file', [File]),
  38.         fail
  39.     ),    
  40.     $load_foreign(Path),
  41.     record_foreigns(Path),
  42.  
  43.     statistics(heapused, Heap),
  44.     statistics(cputime, Time),
  45.     HeapUsed is Heap - OldHeap,
  46.     TimeUsed is Time - OldTime,
  47.  
  48.     confirm_files(File, CFs),
  49.     list_to_atom(CFs, CF),
  50.     context_module(M),
  51.     module_spec(M, ModSpec),
  52.     $ttyformat('Foreign file `~w'' loaded~w, ~2f seconds, ~D bytes~n',
  53.            [CF, ModSpec, TimeUsed, HeapUsed]).
  54.  
  55.  
  56. %    load_foreign(+Files, +Entry)
  57.  
  58. load_foreign(Files, Entry) :-
  59.     statistics(heapused, OldHeap),
  60.     statistics(cputime, OldTime),
  61.  
  62.     check_files(Files, Paths),
  63.     list_to_atom(Paths, Atom),
  64.     confirm_files(Files, CFs),
  65.     list_to_atom(CFs, CF),
  66.     tmp_file(ld, Object),
  67.     import_file(Imports),
  68.     concat_atom(['cc -bI:', Imports,
  69.              ' -e ', Entry,
  70.              ' -o ', Object,
  71.              ' ', Atom], Cmd),
  72.     concat_atom(['cc -bI:', Imports,
  73.              ' -e ', Entry,
  74.              ' -o ', Object,
  75.              ' ', CF], UCmd),
  76.     format(user_output, '~w~n', [UCmd]),
  77.     (    shell(Cmd)
  78.     ->   $load_foreign(Object),
  79.          delete_file(Object)
  80.     ;    $warning('load_foreign/2: failed ~w', Cmd),
  81.          fail
  82.     ),
  83.     record_foreigns(Paths),
  84.  
  85.     statistics(heapused, Heap),
  86.     statistics(cputime, Time),
  87.     HeapUsed is Heap - OldHeap,
  88.     TimeUsed is Time - OldTime,
  89.  
  90.     context_module(M),
  91.     module_spec(M, ModSpec),
  92.     $ttyformat('Foreign file(s) ~w loaded~w, ~2f seconds, ~D bytes~n',
  93.            [CF, ModSpec, TimeUsed, HeapUsed]).
  94.     
  95.  
  96. %    import_file(-File)
  97. %    Return path-name to file with import declarations
  98.  
  99. import_file(File) :-
  100.     feature(home, Home),
  101.     concat(Home, '/include/SWI-Exports', File).
  102.  
  103. %    foreign_file(?File)
  104. %    Is true if 'File' is loaded as a foreign file.
  105.  
  106. foreign_file(File) :-
  107.     recorded($foreign_file, File).
  108.  
  109. module_spec(user, '') :- !.
  110. module_spec(M, S) :-
  111.     sformat(S, ' into ~w', [M]).
  112.  
  113. confirm_files([], []) :- !.
  114. confirm_files([H|T], [CH|CT]) :- !,
  115.     confirm_file(H, CH),
  116.     confirm_files(T, CT).
  117. confirm_files(F, CF) :-
  118.     confirm_file(F, CF).
  119.  
  120. confirm_file(library(File), Confirm) :- !,
  121.     check_files(library(File), Confirm).
  122. confirm_file(File, File).
  123.  
  124. load_foreign_(Files, Entry, Options, Libraries, Size) :-
  125.     check_files(Files, Expanded),
  126.     list_to_atom(Expanded, F),    
  127.     list_to_atom(Options, O),
  128.     list_to_atom(Libraries, L),
  129.  
  130.     $load_foreign(F, Entry, O, L, Size),
  131.     record_foreigns(Expanded).
  132.  
  133. check_files(0, _) :- !, fail.        /* variable file name */
  134. check_files([], []) :- !.
  135. check_files([F|R], [A|T]) :- !,
  136.     check_files(F, A),
  137.     check_files(R, T).
  138. check_files(F, A) :-
  139.     absolute_file_name(F,
  140.                [ extensions(['.o', '.a', '.c', '']),
  141.                  access(read)
  142.                ], A), !.
  143. check_files(F, _) :-
  144.     $warning('~w: No such foreign file', [F]),
  145.     fail.
  146.  
  147. list_to_atom(Atom, Atom) :-
  148.     atomic(Atom), !.    
  149. list_to_atom(List, Atom) :-
  150.     insert_spaces(List, SPList),    
  151.     concat_atom(SPList, Atom).
  152.  
  153. insert_spaces([One], [One]) :- !.
  154. insert_spaces([H|T], [H, ' '| R]) :-
  155.     insert_spaces(T, R).
  156.  
  157. record_foreigns([]) :- !.
  158. record_foreigns([H|T]) :- !,
  159.     record_foreigns(H),
  160.     record_foreigns(T).
  161. record_foreigns(F) :-
  162.     absolute_file_name(F, Path),
  163.     (   recorded($foreign_file, Path)
  164.     ;   recordz($foreign_file, F)
  165.     ), !.
  166.